package com.sergiandreplace.kaboom.format;

import java.util.Random;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;

import com.sergiandreplace.kaboom.format.FormatManager.MessageHandler;

public class FormatBitmapExtractorThread extends java.lang.Thread
{

	private static final int MAX_WORKS = 1;

	private static int works = 0;
	Random rand = new Random();

	
	public String getFilename()
	{
		return filename;
	}

	public void setFilename(String filename)
	{
		this.filename = filename;
	}

	public MessageHandler getMessageHandler()
	{
		return messageHandler;
	}

	public void setMessageHandler(MessageHandler messageHandler)
	{
		this.messageHandler = messageHandler;
	}

	public int getSize()
	{
		return size;
	}

	public void setSize(int size)
	{
		this.size = size;
	}

	String filename;
	MessageHandler messageHandler;
	int size;
	public FormatBitmapExtractorThread(String filename, int size, MessageHandler messageHandler)
	{
		this.filename = filename;
		this.messageHandler = messageHandler;
		this.size = size;
	}
	
	@Override
	public void run()
	{
		super.run();
		while (works >= MAX_WORKS)
			try
			{
				sleep(rand.nextInt(400));
			}
			catch (InterruptedException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			};
		works++;

	}
	
	public void finish(Bitmap result, int what)
	{
		Message msg = Message.obtain();
		msg = Message.obtain();
		Bundle b = new Bundle();
		b.putParcelable("Bitmap", result);
		msg.what = what;
		msg.setData(b);
		messageHandler.sendMessage(msg);
		works--;
	}
	
}
